/*
     raw os - Copyright (C)  Lingjun Chen(jorya_txj).

    This file is part of raw os.

    raw os is free software; you can redistribute it it under the terms of the 
    GNU General Public License as published by the Free Software Foundation; 
    either version 3 of the License, or  (at your option) any later version.

    raw os is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
    without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
    See the GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program. if not, write email to jorya.txj@gmail.com
                                      ---

    A special exception to the LGPL can be applied should you wish to distribute
    a combined work that includes raw os, without being obliged to provide
    the source code for any proprietary components. See the file exception.txt
    for full details of how and when the exception can be applied.
*/


/* 	2012-9  Created by dezhi jiang
  *	xxxxxx   please added here
  */


#include "def.h"
#include "option.h"
#include "2440addr.h"     
#include "2440lib.h"
#include "2440slib.h"     
#include "mmu.h" 
#include "timer.h"

#include "raw_api.h"
#include <mm/raw_page.h>
#include <mm/raw_malloc.h>

#undef U32

#include "gui.h"
#include "FRAMEWIN.h"
#include "GUI_X.h"
#include "lcd_touch.h" 
#define  TEST_TASK_STK_SIZE 4048


RAW_TIMER timer_touch;

PORT_STACK test_gui_stack1[TEST_TASK_STK_SIZE];

PORT_STACK test_gui_stack2[TEST_TASK_STK_SIZE];
PORT_STACK test_gui_stack3[TEST_TASK_STK_SIZE];

RAW_TASK_OBJ 		test_gui_obj[3];

extern RAW_U8 test_started;

extern void GUIDEMO_main(void);
extern void Touch_init(void);
extern void Timer0Init(void);

void timer_touch_fun(RAW_VOID *para)
{
	GUI_TOUCH_Exec();
}

int g1,g2,g3;
static int XPos;
static const char aText[] = "Moving text...";

static void _cbCallbackT0(WM_MESSAGE * pMsg) {
  switch (pMsg->MsgId) {
  case WM_PAINT:
    /* Handle the paint message */
    GUI_SetBkColor(GUI_RED);
    GUI_SetColor(GUI_BLACK);
    GUI_SetFont(&GUI_FontComic24B_ASCII);
    GUI_Clear();
    GUI_DispStringAt(aText, XPos, 0);
    break;
  default:
    WM_DefaultProc(pMsg);
  }
}

/*******************************************************************
*
*       _cbCallbackT1
*/
static void _cbCallbackT1(WM_MESSAGE * pMsg) {
  WM_HWIN hWin = (FRAMEWIN_Handle)(pMsg->hWin);
  switch (pMsg->MsgId) {
  case WM_PAINT:
    /* Handle the paint message */
    GUI_SetBkColor(GUI_BLUE);
    GUI_SetColor(GUI_WHITE);
    GUI_SetFont(&GUI_FontComic24B_ASCII);
    GUI_SetTextAlign(GUI_TA_HCENTER | GUI_TA_VCENTER);
    GUI_Clear();
    GUI_DispStringHCenterAt("Moving window...", 
                            WM_GetWindowSizeX(hWin) / 2, 
                            WM_GetWindowSizeY(hWin) / 2);
    break;
  default:
    WM_DefaultProc(pMsg);
  }
}

/*******************************************************************
*
*       _cbBackgroundWin
*/
static void _cbBackgroundWin(WM_MESSAGE* pMsg) {
  switch (pMsg->MsgId) {
  case WM_PAINT:
    /* Handle only the paint message */
    GUI_SetBkColor(0x00CC00);
    GUI_Clear();
    GUI_SetFont(&GUI_Font24_ASCII);
    GUI_DispStringHCenterAt("emWinGSC - multitasking demo\n", 160, 5);
    GUI_SetFont(&GUI_Font13_1);
    GUI_DispStringAt("Scrolling text and moving windows without flickering", 5, 35);
  default:
    WM_DefaultProc(pMsg);
  }
}

void test1_gui1(void * pParam) 
{
	RAW_U8 a = 0x55;
	FRAMEWIN_Handle hFrameWin ;
	WM_HWIN         hChildWin;
	Timer0Init();
	Touch_init();
	raw_timer_create(&timer_touch, (RAW_U8  *)"timer_touch",timer_touch_fun, &a, RAW_TICKS_PER_SECOND/100,RAW_TICKS_PER_SECOND/100,1);
//	raw_timer_activate(&timer_touch);
	/* Create frame window */
	hFrameWin = FRAMEWIN_Create("Task 0",  NULL, WM_CF_SHOW | WM_CF_STAYONTOP,  0, 70, 200, 40);
	/* Create child window */
	hChildWin = WM_CreateWindowAsChild(0, 0, 0, 0, WM_GetClientWindow(hFrameWin), 
	                                                 WM_CF_SHOW | WM_CF_MEMDEV,  _cbCallbackT0, 0);
	
	FRAMEWIN_SetActive(hFrameWin, 0);
	/* Make sure the right window is active... */
	WM_SelectWindow(hChildWin);
	/* ...and the right font is selected */
	GUI_SetFont(&GUI_FontComic24B_ASCII);
	 GUI_CURSOR_Show();
	while(1) {
		GUI_RECT Rect;
		int XLen = GUI_GetStringDistX(aText);   /* Get the length of the string */
			g1++;
		WM_GetClientRect(&Rect);                /* Get the size of the window */
		/* Show moving text */
		for (XPos = 0; XPos < (Rect.x1 - Rect.x0) - XLen; XPos++) {
		  WM_InvalidateWindow(hChildWin);
		  raw_sleep(5);
		}
		for (; XPos >= 0; XPos--) {
		  WM_InvalidateWindow(hChildWin);
		  raw_sleep(10);
		}
	}
}

void test1_gui2(void * pParam) 
{
  /* Create frame window */
  FRAMEWIN_Handle hFrameWin = FRAMEWIN_Create("Task 1", NULL, WM_CF_SHOW | WM_CF_STAYONTOP, 
                                              20, 170, 200, 40);
  /* Create child window */
  WM_HWIN hChildWin = WM_CreateWindowAsChild(0, 0, 0, 0, WM_GetClientWindow(hFrameWin), WM_CF_SHOW | WM_CF_MEMDEV,
                                             _cbCallbackT1, 0);
  FRAMEWIN_SetActive(hFrameWin, 0);
  while(1) {
    int i;
    int nx = 80;
    int ny = 90;
		g2++;
    /* Move window continously */
    for (i = 0; i < ny; i++) {
      WM_MoveWindow(hFrameWin, 0, -2);
      raw_sleep(5);
    }
    for (i = 0; i < nx; i++) {
      WM_MoveWindow(hFrameWin, 2, 0);
      raw_sleep(5);
    }
    for (i = 0; i < ny; i++) {
      WM_MoveWindow(hFrameWin, 0, 2);
      raw_sleep(5);
    }
    for (i = 0; i < nx; i++) {
      WM_MoveWindow(hFrameWin, -2, 0);
      raw_sleep(5);
    }
  }
}
void test1_gui3(void * pParam) 
{
  while(1) {
    GUI_Exec();           /* Do the background work ... Update windows etc.) */
    GUI_X_ExecIdle();     /* Nothing left to do for the moment ... Idle processing */
  }
}



void gui_test()
{

	if (test_started) {

		return;

	}

	test_started = 1;

	GUI_Init();
	WM_SetCallback(WM_HBKWIN, _cbBackgroundWin);  /* Set callback for background window */

	raw_task_create(&test_gui_obj[0], (RAW_U8  *)"gui1", 0,
	                         30, 0,  test_gui_stack1, 
	                         TEST_TASK_STK_SIZE , test1_gui1, 1); 

	raw_task_create(&test_gui_obj[1], (RAW_U8  *)"gui2", 0,
	                         35, 0,  test_gui_stack2, 
	                         TEST_TASK_STK_SIZE , test1_gui2, 1);

	raw_task_create(&test_gui_obj[2], (RAW_U8  *)"gui3", 0,
	                         40, 0,  test_gui_stack3, 
	                         TEST_TASK_STK_SIZE , test1_gui3, 1);  

}


